#include "string.h"
namespace hj_string
{
	const size_t string::npos = -1;
	void string::reverse(size_t capacity)
	{
		//cout << _capacity << endl;
		if (capacity <= _capacity)
		{
			return;
		}
		char* tmp = new char[capacity+1];
		strcpy(tmp, _str);
		_capacity = capacity;
		delete[] _str;
		_str = tmp;
	}

	const char* string::c_str() const
	{
		return _str;
	}

	void string::push_back(const char& c)
	{
		if (_size == _capacity)
		{
			reverse(_capacity == 0 ? 4 : _capacity * 2);
		}
		_str[_size++] = c;
		_str[_size] = '\0';
	}

	string& string::append(const string& str)
	{
		for (auto ch : str)
		{
			push_back(ch);
		}
		return *this;
	}
	string& string::operator+=(const string& str)
	{
		append(str);
		return *this;
	}
	string& string::operator+=(const char& c)
	{
		push_back(c);
		return *this;
	}
	string& string::insert(size_t pos, const char& c)
	{
		assert(pos <= _size);
		if (_size == _capacity)
		{
			reverse(_capacity == 0 ? 4 : _capacity * 2);
		}
		size_t end = _size;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			end++;
		}
		_str[pos] = c;
		_size++;
		_str[_size] = '\0';
		return *this;
	}
	string& string::insert(size_t pos, const string& str)
	{
		size_t len = str._size;
		if (_size + len >= _capacity)
		{
			if (_size + len > _capacity * 2)
			{
				reverse(_size + len);
			}
			else
			{
				reverse(_capacity * 2);
			}
		}
		size_t end = _size + len - 1;
		while (end < pos + len)
		{
			_str[end] = _str[end - len];
			end--;
		}
		for (auto ch : str)
		{
			_str[pos++] = ch;
		}
		_size = _size + str._size;
		_str[_size] = '\0';
		return *this;
	}
	string& string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len == npos || len >= _size-pos )
		{
			_size = pos;
			_str[_size] = '\0';
			return *this;
		}

		size_t end = pos + len;
		while (end <= _size)
		{
			_str[end - len] = _str[end];
			end++;
		}
		_size = _size - len;
		_str[_size] = '\0';
		return *this;
	}

	char& string::operator[](size_t pos)
	{
		return _str[pos];
	}

	char& string::operator[](size_t pos) const
	{
		return _str[pos];
	}


	size_t string::size()
	{
		return _size;
	}
	size_t string::capacity()
	{
		return _capacity;
	}
	void string::clear()
	{
		_size = 0;
		_str[_size] = '\0';
	}
	bool string::empty()
	{
		return _size == 0;
	}

	size_t string::find(const char& ch, size_t pos)
	{
		if (pos >= _size)
		{
			return npos;
		}
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}
		return npos;
	}

	size_t string::find(const string& str, size_t pos)
	{
		if (pos >= _size)
		{
			return npos;
		}
		for (size_t i = pos; i < _size; i++)
		{
			if (strncmp(_str+i,str._str,str._size) == 0)
			{
				return i;
			}
		}
		return npos;
	}

	string string::substr(size_t pos, size_t len)
	{
		assert(pos <= _size);
		string str;
		if (len == npos)
		{
			str += _str + pos;
		}
		else
		{
			for (size_t i = pos; i < pos + len; i++)
			{
				str += _str[i];
			}
		}
		return str;
	}

	ostream& operator<<(ostream& out, const string& str)
	{
		for (auto ch : str)
		{
			out << ch;
		}
		return out;
	}
	istream& operator>>(istream& in, string& str)
	{
		char buffer[256];
		char c;
		int i = 0;
		in.get(c);
		while (c != ' ' && c != '\n')
		{
			if (i == 255)
			{
				buffer[255] = '\0';
				str += buffer;
				i = 0;
			}
			buffer[i++] = c;
			in.get(c);
		}
		buffer[i] = '\0';
		str += buffer;
		return in;
	}

	bool operator<(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str());
	}
	bool operator<=(const string& s1, const string& s2)
	{
		return s1 == s2 || s1 < s2;
	}
	bool operator>(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str());
	}
	bool operator>=(const string& s1, const string& s2)
	{
		return s1 == s2 || s1 > s2;
	}
	bool operator==(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str());
	}
	bool operator!=(const string& s1, const string& s2)
	{
		return !(s1 == s2);
	}

	void TestStringMemoryFunc()
	{
		string s;
		string s1("test.cpp.cpp");

		string s2(s1);

		s = s2;

		//cout << s1.c_str() << endl;
		//cout << s1.find('.') << endl;
		//cout << s1.find("llo") << endl;
		string str = s1.substr(s.find('.'));
		cout << str << endl;
	}

	void TestStringNonMemoryFunc()
	{
		string s1("hello world");

		s1.erase(0, 10);
		cout << s1 << endl;
	}

	void TestStringModifiers()
	{
		string s1;
		string s2("hj");

		s1.push_back('a');
		s1.push_back('a');
		s1.push_back('a');
		s1.push_back('a');
		cout << s1.c_str() << endl;

		s1.append("hello ");
		cout << s1.c_str() << endl;
		s1.append(s2);
		cout << s1.c_str() << endl;

		s1 += s2;
		s1 += s2;
		s1 += '*';
		cout << s1.c_str() << endl;

		s1.insert(0, "&&&");
		cout << s1.c_str() << endl;

		s1.insert(0, "%");
		cout << s1.c_str() << endl;

		s1.erase(4);
		cout << s1.c_str() << endl;

		int  count = 0;
		while (1)
		{
			s1.insert(0,"huangjian");
			s1.erase(0, 9);
			cout << s1.capacity() << endl;
			if (count++ == 100)break;
		}
	}
	void TestStringIterator()
	{
		string s1("hello world");
		for (auto ch : s1)
		{
			cout << ch;
		}
		cout << endl;

		string::iterator it = s1.begin();
		while (it != s1.end())
		{
			cout << *it << "  ";
			it++;
		}
		cout << endl;

		string::const_iterator cit = s1.begin();
		while (cit != s1.end())
		{
			cout << *cit << "  ";
			cit++;
		}
		cout << endl;

	}


	void TestStringCapacity()
	{
		string s1("hello world");
		cout << s1.size() << endl;

		cout << s1.capacity() << endl;

		cout << s1.empty() << endl;

		s1.clear();
		cout << s1.size() << endl;
		cout << s1.empty() << endl;


	}
}